//
//  GroupNoticeViewController.m
//  Findif
//
//  Created by Mac on 2019/2/25.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "GroupNoticeViewController.h"
#import "GroupNoticeTableViewCell.h"
#import "GroupNoticeTopTableViewCell.h"
#import "PushGroupNoticeViewController.h"
#import "NoticeModel.h"

@interface GroupNoticeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <NoticeModel *> * noticeArray;
@property (nonatomic, weak)UIButton * rightButton;

@end

@implementation GroupNoticeViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self loadData];
}


- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getGroupNoticeListWithGroup_id:self.group_id WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.noticeArray = [NSArray yy_modelArrayWithClass:[NoticeModel class] json:responseData[@"data"]];
        }
    }];
}


- (void)setNoticeArray:(NSArray<NoticeModel *> *)noticeArray{
    
    _noticeArray = noticeArray;
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNav];
    [self setupUI];
}


- (void)setupNav{
    
    self.title = @"社团公告";
    
    UIButton * rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20 * SCREENH6, 20 * SCREENH6)];
    
    [rightButton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didSetUpButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.rightButton = rightButton;
    
    UIBarButtonItem * rightBar = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBar;
    
    if ([self.identity isEqualToString:@"1"]) {
        
        self.rightButton.hidden = NO;
    }else{
        
        self.rightButton.hidden = YES;
    }
}

- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF4F5F6);
//    tableView.rowHeight = 135 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;

    self.tableView = tableView;
    
    [self.tableView registerClass:[GroupNoticeTableViewCell class] forCellReuseIdentifier:@"GroupNoticeTableViewCell"];
    [self.tableView registerClass:[GroupNoticeTopTableViewCell class] forCellReuseIdentifier:@"GroupNoticeTopTableViewCell"];
    
    //    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //
    //        [self loadData];
    //    }];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
    }else{
        
        return self.noticeArray.count - 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        
        GroupNoticeTopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupNoticeTopTableViewCell" forIndexPath:indexPath];
        
        cell.model = self.noticeArray[indexPath.row];
        
        return cell;
    }else{
        
        GroupNoticeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupNoticeTableViewCell" forIndexPath:indexPath];
        
        cell.model = self.noticeArray[indexPath.row + 1];
        
        return cell;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
//        return 60 * SCREENH6;
        NoticeModel * model = self.noticeArray[indexPath.row];
        return model.rowHeight;
    }else{
        
        NoticeModel * model = self.noticeArray[indexPath.row + 1];
        return model.rowHeight;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    if (indexPath.section == 0) {
        
        if ([self.identity isEqualToString:@"1"]) {
            
            NoticeModel * model = self.noticeArray[indexPath.row];
            
            PushGroupNoticeViewController * pushVC = [PushGroupNoticeViewController new];
            pushVC.group_id = self.group_id;
            pushVC.model = model;
            
            [self.navigationController pushViewController:pushVC animated:YES];
        }
    }else{
        
        NoticeModel * model = self.noticeArray[indexPath.row + 1];
        
        PushGroupNoticeViewController * pushVC = [PushGroupNoticeViewController new];
        pushVC.group_id = self.group_id;
        pushVC.model = model;
        
        [self.navigationController pushViewController:pushVC animated:YES];
    }
    
    
}


- (void)didSetUpButton{
    
    HiddenBar;
    
    PushGroupNoticeViewController * pushVC = [PushGroupNoticeViewController new];
    
    pushVC.group_id = self.group_id;
    
    [self.navigationController pushViewController:pushVC animated:YES];
}

@end
